// ActionScript file
// Just a test statmeent
import Components.TitleWindow.twProductCategoryTree;
import Components.TitleWindow.twProductPrice;
import Components.TitleWindow.twQuantityUnit;
import Components.TitleWindow.twSaveSupplier;

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.ProductCategory;
import org.flexlancer.databases.Products;
import org.flexlancer.databases.QuantityUnit;
import org.flexlancer.databases.Suppliers;
import org.flexlancer.events.PriceAddedEvent;
import org.flexlancer.events.SelectedFromListEvent;

[Bindable]
private var dpProductCategories:Array;

[Bindable]
private var dpQuantityUnits:Array;

[Bindable]
private var dpProductPrice:Array;

[Bindable]
private var dpSupplier:Array;

private var productTitle:String;
private var barcode:String;
private var description:String;
private var instock:Number;
private var orderLevel:Number;
private var cost:Number;


private function OnCC():void{
	try{
		this.dpQuantityUnits = QuantityUnit.List();
		this.dpProductPrice = new Array();
		this.dpSupplier = Suppliers.List();
		
		if ( this.editObject != null ){
			this.txtProductTitle.text = this.editObject.title;
			this.txtBarCode.text = this.editObject.barcode;
			this.txtDescription.text = this.editObject.desc;
			this.txtInStock.text = this.editObject.instock;
			this.txtOrderLevel.text = this.editObject.orderlevel;
			this.txtCost.text = this.editObject.cost;
			
			this.dpProductCategories = ProductCategory.List("catid="+this.editObject.refproducttype);
			this.txtCategory.text = this.dpProductCategories[0].catname;
						
			for each ( var singleSup:Object in this.dpSupplier ){
				if ( singleSup.supplierid == this.editObject.refsupplier){
					this.cmbSupplier.selectedItem = singleSup;
					break;
				}
			}

			for each ( var singleQU:Object in this.dpQuantityUnits ){
				if ( singleQU.qunitid == this.editObject.refunit){
					this.cmbQuantityUnit.selectedItem = singleQU;
					break;
				}
			}
			this.dpProductPrice = this.editObject.prices;
			this.dgPrices.dataProvider = this.dpProductPrice;			
		}		 
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private var editObject:Object = null;

public function set EditObject(val:Object):void{
	try{
		this.editObject = val;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddCategory():void{
	try{
		var addCat:twProductCategoryTree = PopUpManager.createPopUp(this,twProductCategoryTree, true) as twProductCategoryTree;
		PopUpManager.centerPopUp(addCat);
		addCat.addEventListener(SelectedFromListEvent.SELECTED,AddCategory_OnSelected);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private var cat_id:String = "";
private function AddCategory_OnSelected(evt:SelectedFromListEvent):void{
	try{
		var lastIndex:Number = evt.Data.indexOf("-");
		this.cat_id = evt.Data.substr(0,lastIndex);
		this.txtCategory.text = evt.Data.substr(lastIndex+1);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnAddSupplier():void{
	try{
		var addSupplier:twSaveSupplier = PopUpManager.createPopUp(this,twSaveSupplier,true) as twSaveSupplier;
		PopUpManager.centerPopUp(addSupplier);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddSuggestedQuantityUnit():void{
	try{
		var addUnit:twQuantityUnit = PopUpManager.createPopUp(this,twQuantityUnit, true) as twQuantityUnit;
		PopUpManager.centerPopUp(addUnit);
		addUnit.addEventListener(CloseEvent.CLOSE,QuantityUnit_OnClose);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function QuantityUnit_OnClose(evt:CloseEvent):void{
	try{
		this.dpQuantityUnits = QuantityUnit.List();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnAddProductPrice():void{
	try{
		var addPrice:twProductPrice = PopUpManager.createPopUp(this,twProductPrice,true) as twProductPrice ;
		addPrice.pricelist = this.dpProductPrice;
		addPrice.costprice = Number(this.txtCost.text);
		addPrice.addEventListener(PriceAddedEvent.PRICE_ADDED,OnAddPrice);
		PopUpManager.centerPopUp(addPrice);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddPrice(evt:PriceAddedEvent):void{
	try{
		this.dpProductPrice.push(evt.PriceObject);
		this.dgPrices.dataProvider = this.dpProductPrice;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function PerformDeletePrice():void{
	try{
		if ( this.dgPrices.selectedItem != null ){
			var index:Number = this.dpProductPrice.indexOf(this.dgPrices.selectedItem);
			this.dpProductPrice.splice(index,1);
			this.dgPrices.dataProvider = this.dpProductPrice;
		}else{
			Alert.show("Please select a price to delete.","No price selected");
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

override protected function OnKeyUp(evt:KeyboardEvent):void{
	try{
		super.OnKeyUp(evt);
		if ( evt.keyCode == Keyboard.DELETE ){
			this.PerformDeletePrice();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnSaveClick():void{
	try{
		var a:Boolean,b:Boolean,c:Boolean,d:Boolean,e:Boolean,f:Boolean,g:Boolean = false;
		
		if(this.txtProductTitle.text.length<1){
			Alert.show("Title Field is Empty","Empty Field");
		}
		else
		{
			this.productTitle = this.txtProductTitle.text;
			a=true;
		}
		
		if(this.txtBarCode.text.length<1){
			Alert.show("BarCode Field is Empty","Empty Field");
		}
		else
		{
			this.barcode = this.txtBarCode.text;
			b=true;
		}
		
		if(this.txtDescription.text.length<1){
			Alert.show("Description Field is Empty","Empty Field");
			
		}
		else
		{
			this.description = this.txtDescription.text;
			c=true;
		}
		
		if(this.txtInStock.text.length<1){
			Alert.show("In-Stock Field is Empty","Empty Field");
		}
		else
		{
			this.instock = Number(this.txtInStock.text);
			d=true;
		}
		
		if(this.txtOrderLevel.text.length<1){
			Alert.show("Order Level Field is Empty","Empty Field");
		}
		else
		{
			this.orderLevel = Number(this.txtOrderLevel.text);
			e=true;
		}
		
		if(this.txtCost.text.length<1){
			Alert.show("Cost Field is Empty","Empty Field");
		}
		else
		{
			this.cost = Number(this.txtCost.text);
			f=true;
		}
		
		if ( this.cat_id == "" ){
			Alert.show("Category is not selected","Not selected");
		}else{
			g = true;
		}

		if( a && b && c && d && e && f && g){
			if ( this.editObject == null ){
				Products.Add(this.productTitle, 
				this.barcode, 
				this.description, 
				this.instock, 
				this.orderLevel, 
				this.cost, 
				this.cat_id,
				this.cmbSupplier.selectedItem.supplierid,
				this.cmbQuantityUnit.selectedItem.qunitid ,
				this.dpProductPrice);			
			}else{
				Products.Update(this.editObject.productid,
				this.productTitle, 
				this.barcode, 
				this.description, 
				this.instock, 
				this.orderLevel, 
				this.cost, 
				this.cat_id,
				this.cmbSupplier.selectedItem.supplierid,
				this.cmbQuantityUnit.selectedItem.qunitid ,
				this.dpProductPrice);			
			}
		}
		
		//Customers.AddCustomer(this.custName, this.custType , this.custInfo , this.vehicleInfo);
		if ( this.editObject == null ){
			if(this.chkAddAnother.selected){
				this.txtProductTitle.text = "";
				this.txtBarCode.text = "";
				this.txtDescription.text = "";
				this.txtInStock.text = "";
				this.txtOrderLevel.text = "";
				this.txtCost.text = "";
				this.dpProductPrice = new Array();
			}else{
				PopUpManager.removePopUp(this);
			}	
		}else{
			PopUpManager.removePopUp(this);
		}
				
	}catch(err:Error){
		Logger.LogE(err);
	}
}